package com.baige.api.app.controller;

import com.baige.api.app.sdk.request.*;
import com.baige.api.app.sdk.response.UserGetFaceCertificationUrlResponse;
import com.baige.api.app.sdk.response.UserResponse;
import com.baige.common.core.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

/**
 * 用户接口
 * @author wulg
 * @date 2018/12/12.
 */
@Api(value = "UserController", description = "用户接口", produces = org.springframework.http.MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/api/rest/user")
public class UserController extends BaseApiController
{

    private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    /**
     * 用户手机号登陆（如不存在则直接注册）
     *
     * @param requestParams 用户手机登陆请求参数对象
     * @param bindingResult 参数校验对象
     * @return Result<UserLoginResponse>
     */
    @PostMapping(value = "/v1/phone/login", produces = {"application/json;charset=UTF-8", "application/xml;charset=UTF-8"})
    @ApiOperation(value = "用户手机号登陆（如不存在则直接注册）", notes = "用户手机号登陆（如不存在则直接注册）", httpMethod = "POST", produces = org.springframework.http.MediaType.APPLICATION_JSON_VALUE)
    public Result<UserResponse> phoneLogin(@Valid @RequestBody UserPhoneLoginRequest requestParams, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            logger.error("用户手机号登陆参数传递错误,message:{}", bindingResult.getFieldErrors().get(0).getDefaultMessage());
            return BaseResponse.newErrorParamReturnObject(bindingResult.getFieldErrors().get(0).getDefaultMessage());
        }
        return null;
    }

    /**
     * 修改个人资料
     * @param userId        用户id
     * @param requestParams 参数对象
     * @param bindingResult 参数校验对象
     * @return Result<MsgResponse>
     */
    @PutMapping(value = "/v1/{userId}", produces = {"application/json;charset=UTF-8", "application/xml;charset=UTF-8"})
    @ApiOperation(value = "修改个人资料", notes = "修改个人资料（头像和昵称）", httpMethod = "PUT", produces = org.springframework.http.MediaType.APPLICATION_JSON_VALUE)
    public Result<UserResponse> modifyUser(@Valid @RequestBody UserModifyRequest requestParams, BindingResult bindingResult, @PathVariable("userId") String userId) {
        if (bindingResult.hasErrors()) {
            logger.error("用户修改个人资料参数传递错误,message:{}", bindingResult.getFieldErrors().get(0).getDefaultMessage());
            return BaseResponse.newErrorParamReturnObject(bindingResult.getFieldErrors().get(0).getDefaultMessage());
        }
        return null;
    }

    /**
     * 绑定第三方账号
     *
     * @param requestParams 参数对象
     * @param userId        用户id
     * @param bindingResult 参数校验对象
     * @return Result<MsgResponse>
     */
    @PutMapping(value = "/v1/{userId}/thirdPart", produces = {"application/json;charset=UTF-8", "application/xml;charset=UTF-8"})
    @ApiOperation(value = "绑定第三方账号", notes = "绑定第三方账号", httpMethod = "PUT", produces = org.springframework.http.MediaType.APPLICATION_JSON_VALUE)
    public Result<MsgResponse> bindThirdPartAccount(@Valid @RequestBody UserBindThirdPartAccountRequest requestParams, @PathVariable("userId") String userId, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            logger.error("用户绑定第三方账号参数传递错误,message:{}", bindingResult.getFieldErrors().get(0).getDefaultMessage());
            return BaseResponse.newErrorParamReturnObject(bindingResult.getFieldErrors().get(0).getDefaultMessage());
        }
        return null;
    }

    /**
     * 解绑第三方账号
     *
     * @param requestParams 参数对象
     * @param userId        用户id
     * @param bindingResult 参数校验对象
     * @return Result<MsgResponse>
     */
    @PutMapping(value = "/v1/{userId}/thirdPart/unbind", produces = {"application/json;charset=UTF-8", "application/xml;charset=UTF-8"})
    @ApiOperation(value = "解绑第三方账号", notes = "解绑第三方账号", httpMethod = "PUT", produces = org.springframework.http.MediaType.APPLICATION_JSON_VALUE)
    public Result<MsgResponse> unBindThirdPartAccount(@Valid @RequestBody UserUnBindThirdPartAccountRequest requestParams, @PathVariable("userId") String userId, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            logger.error("用户解绑第三方账号参数传递错误,message:{}", bindingResult.getFieldErrors().get(0).getDefaultMessage());
            return BaseResponse.newErrorParamReturnObject(bindingResult.getFieldErrors().get(0).getDefaultMessage());
        }
        return null;
    }


    /**
     * 第三方登录
     *
     * @param requestParams 参数对象
     * @param bindingResult 参数校验对象
     * @return Result<UserResponse>
     */
    @PostMapping(value = "/v1/thirdPart/login", produces = {"application/json;charset=UTF-8", "application/xml;charset=UTF-8"})
    @ApiOperation(value = "第三方登录", notes = "第三方登录", httpMethod = "POST", produces = org.springframework.http.MediaType.APPLICATION_JSON_VALUE)
    public Result<UserResponse> thirdPartLogin(@Valid @RequestBody UserThirdPartLoginRequest requestParams, BindingResult bindingResult)
    {
        if (bindingResult.hasErrors()) {
            logger.error("用户第三方登录参数传递错误,message:{}", bindingResult.getFieldErrors().get(0).getDefaultMessage());
            return BaseResponse.newErrorParamReturnObject(bindingResult.getFieldErrors().get(0).getDefaultMessage());
        }
        return null;
    }

    /**
     * 第三方注册
     *
     * @param requestParams 参数对象
     * @param bindingResult 参数校验对象
     * @return Result<UserResponse>
     */
    @PostMapping(value = "/v1/thirdPart/register", produces = {"application/json;charset=UTF-8", "application/xml;charset=UTF-8"})
    @ApiOperation(value = "第三方注册", notes = "第三方注册", httpMethod = "POST", produces = org.springframework.http.MediaType.APPLICATION_JSON_VALUE)
    public Result<UserResponse> thirdPartRegister(@RequestHeader("source") int source, @Valid @RequestBody UserThirdPartRegisterRequest requestParams, BindingResult bindingResult,HttpServletRequest request) {
        if (bindingResult.hasErrors()) {
            logger.error("用户第三方注册参数传递错误,message:{}", bindingResult.getFieldErrors().get(0).getDefaultMessage());
            return BaseResponse.newErrorParamReturnObject(bindingResult.getFieldErrors().get(0).getDefaultMessage());
        }
        return null;
    }

    /**
     * 获取用户信息
     *
     * @param userId 用户id
     * @return Result<UserResponse>
     */
    @GetMapping(value = "/v1/{userId}", produces = {"application/json;charset=UTF-8", "application/xml;charset=UTF-8"})
    @ApiOperation(value = "获取用户信息", notes = "获取用户信息", httpMethod = "GET", produces = org.springframework.http.MediaType.APPLICATION_JSON_VALUE)
    public Result<UserResponse> getUserInfo(@PathVariable("userId") String userId) {
        return null;
    }

    /**
     * 用户身份证认证
     * @param userId 用户id
     * @param requestParams 参数对象
     * @param bindingResult 参数校验
     * @return MsgResponse
     */
    @PostMapping(value = "/v1/IDInfo/{userId}", produces = {"application/json;charset=UTF-8", "application/xml;charset=UTF-8"})
    @ApiOperation(value = "用户身份证认证", notes = "用户身份证认证", httpMethod = "POST", produces = org.springframework.http.MediaType.APPLICATION_JSON_VALUE)
    public Result<MsgResponse> realNameCertification(@PathVariable("userId") String userId,@Valid @RequestBody UserRealNameCertificationRequest requestParams,BindingResult bindingResult)
    {
        if (bindingResult.hasErrors()) {
            logger.error("用户身份证认证参数传递错误,message:{}", bindingResult.getFieldErrors().get(0).getDefaultMessage());
            return BaseResponse.newErrorParamReturnObject(bindingResult.getFieldErrors().get(0).getDefaultMessage());
        }
        return null;
    }


    /**
     * 获取支付宝人脸认证的code
     * @param userId 用户id
     * @param requestParams 参数对象
     * @param bindingResult 参数校验
     * @return Result<UserGetFaceCertificationUrlResponse>
     */
    @GetMapping(value = "/v1/IDInfo/parameter/{userId}", produces = {"application/json;charset=UTF-8", "application/xml;charset=UTF-8"})
    @ApiOperation(value = "获取支付宝人脸认证的code", notes = "获取支付宝人脸认证的code", httpMethod = "GET", produces = org.springframework.http.MediaType.APPLICATION_JSON_VALUE)
    public Result<UserGetFaceCertificationUrlResponse> getFaceCertificationUrl(@PathVariable("userId") String userId, @Valid @ModelAttribute("request") UserGetFaceCertificationCodeRequest requestParams, BindingResult bindingResult)
    {
        if (bindingResult.hasErrors()) {
            logger.error("获取支付宝人脸认证的code参数传递错误,message:{}", bindingResult.getFieldErrors().get(0).getDefaultMessage());
            return BaseResponse.newErrorParamReturnObject(bindingResult.getFieldErrors().get(0).getDefaultMessage());
        }
        return null;
    }

    /**
     * 获取支付宝人脸认证回调
     * @param userId 用户id
     * @param requestParams 参数对象
     * @param bindingResult 参数校验
     * @return Result<MsgResponse>
     */
    @GetMapping(value = "/v1/certification/callBack/{userId}", produces = {"application/json;charset=UTF-8", "application/xml;charset=UTF-8"})
    @ApiOperation(value = "获取支付宝人脸认证回调", notes = "获取支付宝人脸认证回调", httpMethod = "GET", produces = org.springframework.http.MediaType.APPLICATION_JSON_VALUE)
    public Result<MsgResponse> callBackFaceCertification(@PathVariable("userId") String userId, @Valid @ModelAttribute("request") UserFaceCertificationCallBackRequest requestParams, BindingResult bindingResult)
    {
        if (bindingResult.hasErrors()) {
            logger.error("获取支付宝人脸认证回调参数传递错误,message:{}", bindingResult.getFieldErrors().get(0).getDefaultMessage());
            return BaseResponse.newErrorParamReturnObject(bindingResult.getFieldErrors().get(0).getDefaultMessage());
        }
        return null;
    }
}
